module.exports = (grunt) ->
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    coffee: coffee,
    compass: compass,
    uglify: uglify,
    watch: watch,
  })
  grunt.loadNpmTasks('grunt-contrib-coffee')
  grunt.loadNpmTasks('grunt-contrib-compass')
  grunt.loadNpmTasks('grunt-contrib-uglify')
  grunt.loadNpmTasks('grunt-contrib-watch')

  grunt.registerTask('default', ['coffee', 'compass', 'uglify', 'watch'])

  # grunt.event.on 'watch', (action, filepath) ->
  #   name = filepath.split(/\/|\\/).slice(-1)[0].split(/\./).slice(0,1)[0]
  #   grunt.task.run ["coffee:#{name}"]

watch = 
  compass: 
    files: ['app/**/*.sass']
    tasks: ['compass']
  coffee: 
    files: ['app/**/*.coffee']
    tasks: ['coffee']

uglify = 
  options: 
    beautify: true
    banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
  build: 
    src: 'build/js/*.js',
    dest: 'build/<%= pkg.name %>.min.js'

compass = 
  dist:           
    options: 
      sassDir: 'app/sass',
      cssDir: 'build/css',
      environment: 'production'

coffee = 
  glob_to_multiple:
    options:
      bare: false
      sourceMap: true
    expand: true
    cwd: 'app'
    src: ['*.coffee']
    dest: 'build/js/'
    ext: '.js'
  # compile:
  #   files:
  #     'build/js/shape.js': 'app/shape.coffee'

# coffee = do (result = {}) ->
#   for name in ['shape', 'global']
#     files = {} ; files["build/js/#{name}.js"] = "app/#{name}.coffee"
#     result[name] = 
#       files: files
#       options:
#         bare: false
#         sourceMap: true
#   result
